﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;

namespace Excel2DbTool.Helpers
{
    public static class PathHelper
    {
        /// <summary>
        /// 获取当前应用程序启动的目录;不包含应用程序名与最后一个System.IO.Path.PathSeparator
        /// 一直到Debug目录;包含Debug目录
        /// </summary>
        public static string CurProcLocation
        {
            get
            {
                return System.Environment.CurrentDirectory;//一直到Debug目录,包含Debug目录
            }
        }
    }

    public static class ObservableCollectionEx
    {
        public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> items)
        {
            foreach (var item in items)
            {
                collection.Add(item);
            }
        }
    }

    public static class CollectionEx
    {
        /// <summary>
        /// 将集合按照给定的个数进行分割
        /// </summary>
        /// <param name="perCount">划分后的每个集合的最大个数</param>
        public static IEnumerable<IEnumerable<T>> SplitByPer<T>(this IEnumerable<T> col, int perCount)
        {
            if (col == null)
            {
                throw new ArgumentNullException();
            }

            int count = col.Count();

            if (count < perCount)
            {
                return new List<IEnumerable<T>>() { col };
            }

            int targetLstCount = (int)Math.Ceiling((count / (perCount * 1.0)));
            var targetLst = new List<IEnumerable<T>>(targetLstCount);

            for (int i = 0; i < targetLstCount; i++)
            {
                var lst = col.Skip(i * perCount).Take(perCount).ToList();
                targetLst.Add(lst);
            }

            return targetLst;

        }
    }

    public static class DebugHelper
    {
        public static void Write(string content)
        {
            MakeSureLogFolderExist();
            content += DateTime.Now.ToString();
            FileHelper.AppendOneLine(@"log\Excel2DbExpcetionMsg.txt", content);
#if DEBUG
#endif
        }

        static void MakeSureLogFolderExist()
        {
            if (!System.IO.Directory.Exists("Log"))
            {
                System.IO.Directory.CreateDirectory("Log");
            }
        }
    }

    public static class FileHelper
    {
        #region Properties
        //系统默认换行符
        public static readonly string NewLine = System.Environment.NewLine;
        #endregion

        #region Methods
        /// <summary>
        /// 如果已存在,则覆盖之
        /// </summary>
        public static void CreateEmptyFile(string fileFullPath)
        {
            //不管是否存在文件夹  直接创建;;;确保所在的文件夹路径一定存在
            var dirPath = Directory.CreateDirectory(Path.GetDirectoryName(fileFullPath));
            File.Create(fileFullPath).Dispose();
        }

        public static void MakeSureFileExsit(string fileFullPath)
        {
            if (File.Exists(fileFullPath))
            {
                return;
            }
            else
            {
                CreateEmptyFile(fileFullPath);
            }
        }

        /// <summary>
        /// 自动在其后插入换行符
        /// </summary>
        public static void AppendOneLine(string fileFullPath, string content)
        {
            if (!File.Exists(fileFullPath))
            {
                CreateEmptyFile(fileFullPath);
            }

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileFullPath, true))
            {
                file.WriteLine(content);
            }
        }

        public static void AppendEmptyLine(string fileFullPath, int countLines = 1)
        {
            for (int i = 0; i < countLines; i++)
            {
                AppendOneLine(fileFullPath, System.Environment.NewLine);
            }
        }

        #endregion
    }

    public static class DigitHelper
    {
        public static bool IsDigit(string str)
        {
            double num;
            bool isNum = double.TryParse(str, out num);
            return isNum;
        }
    }

    public static class DbFieldHelper
    { 
    public static string RemoveQuoto(this string filedValStr)
    {
        return filedValStr.Replace("'","");
    }
    }
}
